package com.wt.controller;


import com.wt.entity.WqunAccount;
import com.wt.entity.WqunCommunity;
import com.wt.entity.WqunCommunityBan;
import com.wt.entity.utilEntity.RewCommunityUserMess;
import com.wt.service.WqunAccountService;
import com.wt.service.WqunCommunityBanService;
import com.wt.service.WqunCommunityService;
import com.wt.service.impl.WqunAccountServiceImpl;
import com.wt.service.impl.WqunCommunityBanServiceImpl;
import com.wt.service.impl.WqunCommunityServiceImpl;
import com.wt.util.R.R;

import java.text.SimpleDateFormat;
import java.util.*;


public class WqunCommunityController {

    WqunCommunityService wqunCommunityService = new WqunCommunityServiceImpl();


    WqunAccountService wqunAdminService = new WqunAccountServiceImpl();


    WqunCommunityBanService wqunCommunityBanService = new WqunCommunityBanServiceImpl();


    //使用一个链队列来保留用户的聊天信息
    LinkedList<WqunCommunity> wqunCommunityLinkedList = new LinkedList<>();


    //社区导论用户信息
    public R communityUserMess(){
        List<WqunAccount> findAllAccountPublic = wqunAdminService.listAll();
        List<RewCommunityUserMess> communityUserMess = new ArrayList<>();
        for (int i = 0; i < findAllAccountPublic.size(); i++) {

            WqunAccount wqunAccount = findAllAccountPublic.get(i);
            RewCommunityUserMess rewCommunityUserMess  =new RewCommunityUserMess();
            //用户基本信息
            rewCommunityUserMess.setId(wqunAccount.getId());
            rewCommunityUserMess.setNickName(wqunAccount.getNickName());
            rewCommunityUserMess.setUserType(wqunAccount.getUserType());
            rewCommunityUserMess.setAvatar(wqunAccount.getAvatar());
            //讨论次数
            int speakNum = wqunCommunityService.countByUid(String.valueOf(rewCommunityUserMess.getId()));
            rewCommunityUserMess.setSpeakNum(speakNum);

            //是否被禁言
            int count = wqunCommunityBanService.isBan(String.valueOf(rewCommunityUserMess.getId()));

            rewCommunityUserMess.setBan(count>=1);
            communityUserMess.add(rewCommunityUserMess);
        }

        return R.ok().data("communityUserMess",communityUserMess);
    }




    //第一请求查询所有社区信息
    public R findAllCommunityFirst(){
        findAllComment();
        return R.ok().data("findAllCommunity",wqunCommunityLinkedList);
    }

    //查询所有社区信息
    public R findAllCommunity(){
//        if(!wqunCommunityIsChange){//信息没有变化，直接说明没有变化
//            return R.ok().data("wqunCommunityIsChange",wqunCommunityIsChange);
//        }
        if(wqunCommunityLinkedList.size() != 0){
            return R.ok().data("findAllCommunity",wqunCommunityLinkedList);
        }else{
            findAllComment();
            return R.ok().data("findAllCommunity",wqunCommunityLinkedList);
        }
    }

    public void findAllComment(){
        List<WqunCommunity> findAllCommunity = wqunCommunityService.findAllComment();
        wqunCommunityLinkedList.clear();
        for (int i = 0; i < findAllCommunity.size(); i++) {
            wqunCommunityLinkedList.addFirst(findAllCommunity.get(i));
        }
    }

    //添加文本信息
    public R addCommunity(WqunCommunity wqunCommunity){
        //判断是否被禁言
        int banCount = wqunCommunityBanService.isBan(wqunCommunity.getUid());
        if(banCount>=1){
            return R.error().message("你已被禁言，请联系管理员解除");
        }


        boolean saveRes = wqunCommunityService.save(wqunCommunity);
        if(saveRes){
            WqunCommunity WqunCommunityById = wqunCommunityService.getLastOne();
            if(wqunCommunityLinkedList.size() >= 50){
                wqunCommunityLinkedList.removeLast();
                wqunCommunityLinkedList.addFirst(WqunCommunityById);
            }else{
                wqunCommunityLinkedList.addFirst(WqunCommunityById);
            }
            return R.ok();
        }else{
            return R.error().message("发送失败");
        }

    }

    public R deleteCommunityById(int id){
        R getByIdrR= wqunCommunityService.getById(id);
        WqunCommunity getById = (WqunCommunity) getByIdrR.getData().get("getById");
        getById.setType("-1");
        boolean deleteRes = wqunCommunityService.updateById(getById);
        if(deleteRes){
            for (int i = 0; i < wqunCommunityLinkedList.size(); i++) {//删除后修改该数据
                if(wqunCommunityLinkedList.get(i).getId() == id){
                    WqunCommunity wqunCommunity = wqunCommunityLinkedList.get(i);
                    wqunCommunity.setType("-1");
                    wqunCommunityLinkedList.set(i,wqunCommunity);
                }
            }
            return R.ok().message("撤回成功");
        }else{
            return R.error().message("撤回失败");
        }
    }

    public R adminDeleteCommunityById(int id){
        R getByIdrR= wqunCommunityService.getById(id);
        WqunCommunity getById = (WqunCommunity) getByIdrR.getData().get("getById");
        getById.setType("-2");
        boolean deleteRes = wqunCommunityService.updateById(getById);
        if(deleteRes){
            for (int i = 0; i < wqunCommunityLinkedList.size(); i++) {//删除后修改该数据
                if(wqunCommunityLinkedList.get(i).getId() == id){
                    WqunCommunity wqunCommunity = wqunCommunityLinkedList.get(i);
                    wqunCommunity.setType("-2");
                    wqunCommunityLinkedList.set(i,wqunCommunity);
                }
            }
            return R.ok().message("撤回成功");
        }else{
            return R.error().message("撤回失败");
        }
    }

}

